package com.fancy.learncenter.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.BookStoreDataBean;
import com.fancy.learncenter.bean.SchoolDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.adapter.FragmentSchoolAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 学堂
 */
public class  SchoolFragment extends Fragment {


    View rootView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_school, container, false);
        ButterKnife.bind(this, rootView);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        ArrayList<SchoolDataBean.ResultBean> data = new ArrayList<>();
        adapter = new FragmentSchoolAdapter(getActivity(), data);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        getSchool();

        return rootView;
    }

    FragmentSchoolAdapter adapter;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getSchool() {

        HttpMehtod.getInstance().getSchool(new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<SchoolDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), SchoolDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<SchoolDataBean>() {
                    @Override
                    public void success(SchoolDataBean dataBean) {
                        adapter.notifyDataSetChanged(dataBean.getResult());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }
}
